package cn.lefull.controller;

import cn.lefull.config.TestAop;
import cn.lefull.entity.Apartment;
import cn.lefull.exception.BusinessException;
import cn.lefull.redis.RedisRepository;
import cn.lefull.service.RedisTest;
import cn.lefull.service.SynchronizedTest;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Set;

/**
 * @description:
 * @auther: qijiang
 * @date: 2018/12/26 10:40
 */

@RestController
@TestAop
//多例
//@Scope(value = "prototype")
public class TestController {

    public static void main(String[] args) {
        String  o = returnT("23213213s");
        System.out.println(o);
    }

    public static <T> T returnT(Object obj) {
        return (T) obj;
    }

    @Resource
    private RedisRepository redisRepository;
    private int a = 1;
    private static int b = 1;
    public static final String KEY_ = "goodsNumber";


    /**
     *
     * @description: redis做高并发下的线程安全-》原理是redis本身就是单线程
     * @author: qijiang @email: jiang1211@foxmail.com
     * @date: 2019/3/19 4:47 PM
     * @param: []
     * @return: java.lang.String
     */
    @GetMapping(value = "/redisTest")
    public String redisTest(){
        redisRepository.set(KEY_,"10",10000L);
        redisRepository.delete("lock");
        Runnable runnable = new RedisTest(redisRepository);
        for (int i = 0; i < 5; i++) {
            Thread thread1 = new Thread(runnable);
            thread1.start();
        }
        return "true";
    }

    /**
     *
     * @description: synchronized 实现线程安全
     * @author: qijiang @email: jiang1211@foxmail.com
     * @date: 2019/3/19 5:39 PM
     * @param: []
     * @return: java.lang.String
     */
    @GetMapping(value = "/synchronizedTest")
    public String synchronizedTest(){
        SynchronizedTest synchronizedTest = new SynchronizedTest();
        for (int i = 0; i < 5; i++) {
            Thread thread1 = new Thread(synchronizedTest);
//            if(i == 3){
//                synchronizedTest.flag=false;
//            }
            thread1.start();
        }
        return "true";
    }
    //@TestAop
    @ApiOperation(value = "【戚江】测试入参拦截", notes = "测试入参拦截")
    @GetMapping(value = "/test1")
    public JSONObject test1(Apartment apartment){
        Set<String> matchings = redisRepository.getMatchings("lefull-apartment-microservice:EmployeeRbac:AllApartment*");
        for (String key:matchings) {
            System.out.println("--------");
            System.out.println(key);
            System.out.println(redisRepository.get(key));
        }
        return new JSONObject();
    }
    //@TestAop
    @ApiOperation(value = "【戚江】测试入参拦截", notes = "测试入参拦截")
    @PostMapping(value = "/testPost1")
    public JSONObject testPost1(Apartment apartment){
        String a ="";
        return new JSONObject();
    }
}
